<template>
	<view>

		<view class="item" style="padding: 0 20rpx 0;">

			<view style="display: inline-flex; height: 80rpx; justify-content: space-between; margin: 0 20rpx;">
				<u-icon name="order" color="#1573FF" label="发送指令" label-color="#333333"
					:customStyle="{'line-height': '80rpx',}">
				</u-icon>

				<text style="line-height: 80rpx; color: #1573FF;font-size: 28rpx;" @click="goRecord">查看历史</text>
			</view>


			<u-line color="#efefef" :customStyle="{'display': 'block',}"></u-line>
			
			<u-form :model="addbean" ref="uform" label-width="auto">

				<u-form-item label="车牌号" >
					<u-input v-model="intentData.carInfo.carNo" inputAlign="right" placeholder="--" disabled
						disabledColor="#ffffff" border="none"></u-input>
				</u-form-item>

				<u-form-item label="设备号" >
					<u-input v-model="intentData.deviceInfo.deviceNo" inputAlign="right" placeholder="--" disabled
						disabledColor="#ffffff" border="none"></u-input>
				</u-form-item>

				<u-form-item label="指令名称" @click="pickerShow(1)">
					<u-input v-model="requestName" inputAlign="right" placeholder="请选择" disabled
						disabledColor="#ffffff" border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item label="参数" @click="pickerShow(2)">
					<u-input v-model="requestParam" inputAlign="right" placeholder="请选择" disabled
						disabledColor="#ffffff" border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				
			</u-form>

			<text style="margin: 20rpx;" class="btn-item" @click="commit">发送指令</text>

		</view>


		<u-picker :show="requestNameShow" :default-selector="[0]" :columns="requestNameList"
			keyName="name" @confirm="pickerConfim" @cancel="requestNameShow=false"></u-picker>
			
		<u-picker :show="requestParamShow" :default-selector="[0]" :columns="deviceInstructSunParamDtoList"
			keyName="sunParamName" @confirm="pickerConfim" @cancel="requestParamShow=false"></u-picker>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				intentData: {},
				addbean:{},
				requestName:'',
				requestParam:'',
				requestNameShow:false,
				requestParamShow:false,
				requestNameList:[],
				pickerType:0,
				deviceInstructSunParamDtoList:[]
			}
		},
		methods: {
			goRecord() {
				getApp().globalData.requestHistory = this.requestNameList[0]
				
				uni.navigateTo({
					url:'/pagesC/requestCar/request-record?deviceId='+this.intentData.deviceInfo.deviceId
				})
			},
			
			//picker是否显示
			pickerShow(type) {
				switch (type) {
					case 1:
						this.requestNameShow = true;
						break
					case 2:
						this.requestParamShow = true;
						break
				}
				this.pickerType = type;
			},
			
			//picker回调函数
			pickerConfim(e) {
				switch (this.pickerType) {
					case 1:
						this.requestNameShow = false;
						// let carInfoObjc = this.carInfoList[index];
						let objc1 = e.value[0];
						
						this.addbean.deviceInstructId = objc1.id;
						this.addbean.deviceInstructParamId = objc1.deviceInstructParamList[0].id;
						this.requestName = objc1.name;
						
						this.deviceInstructSunParamDtoList = [objc1.deviceInstructParamList[0].deviceInstructSunParamDtoList];
						this.requestParam = ''
						break
			
					case 2:
						this.requestParamShow = false;
						
						let objc2 = e.value[0];
						
						this.addbean.deviceInstructSunParamSunId = objc2.id;
						
						this.requestParam = objc2.sunParamName;
						break
				}
			},
			
			commit(){
				let that = this
				
				if(uni.$u.test.isEmpty(this.addbean.deviceInstructId) || uni.$u.test.isEmpty(this.addbean.deviceInstructParamId)){
					this.$u.toast('请选择指令名称')
					return
				}
				
				if(uni.$u.test.isEmpty(this.addbean.deviceInstructId)){
					this.$u.toast('请选择参数')
					return
				}
				
				uni.showModal({
					content: '确定提交吗?',
					success(res) {
						if (res.confirm) {
							that.$u.api.sendinstructions({
								deviceId:that.intentData.deviceInfo.deviceId,
								deviceManufacturerNumber:that.intentData.deviceInfo.deviceManufacturerNumber,
								deviceNo:that.intentData.deviceInfo.deviceNo,
								carId:that.intentData.carInfo.carId,
								carPlateNo:that.intentData.carInfo.carNo,
								deviceInstructId:that.addbean.deviceInstructId,
								deviceInstructParamId:that.addbean.deviceInstructParamId,
								deviceInstructSunParamSunId:that.addbean.deviceInstructSunParamSunId,
								
							}).then(res=>{
								that.$u.toast('提交成功')
							})

						}
					}
				})
				
				
			},

		},
		onLoad(op) {
			this.intentData = getApp().globalData.intentMonitorData

			getApp().globalData.intentMonitorData = {}

			this.$u.api.instructionslist({
				deviceId:this.intentData.deviceInfo.deviceId
			}).then(res=>{
				
				this.requestNameList = [res]
			})
		},
	}
</script>

<style lang="scss" scoped>

</style>
